package com.zretc.controller;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.zretc.news.entity.News;
import com.zretc.news.service.NewsService;
import com.zretc.news.service.impl.NewsServiceImpl;
import com.zretc.tools.PageData;

/**
 * Servlet implementation class NewsServlet
 */
@WebServlet("/NewsServlet")
public class NewsServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	NewsService newsService = new NewsServiceImpl();

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		//
		String op = "";
		if (request.getParameter("op") != null) {
			op = request.getParameter("op");
		}
		System.out.println("op:" + op);
		if ("add".equals(op)) {
			// 添加类型的操作
			doAdd(request, response);
		}else if ("queryByPage".equals(op)) {
			doQueryByPage(request,response);
		}

	}

	/**
	 * 模糊查询 分页
	 * 
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	private void doQueryByPage(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		String keywords = "";
		// 2获取请求参数
		if (request.getParameter("keywords") != null) {
			keywords = request.getParameter("keywords");
		}
		// 页码默认值为1
		int page = 1;
		// 每页记录数默认为10
		int pageSize = 10;
		if (request.getParameter("page") != null) {
			page = Integer.parseInt(request.getParameter("page"));
		}

		if (request.getParameter("pageSize") != null) {
			pageSize = Integer.parseInt(request.getParameter("pageSize"));
		}

		// 3 调用 Service的方法
		PageData<News> pd = newsService.getNewsByPage(page, pageSize, keywords);

		// 4传递PageData对象给jsp，此时传递的是pageData的对象
		request.setAttribute("pd", pd);

		// 4.-1转发回来的时候，我们将查询关键字也存储在request范围内
		request.setAttribute("keywords", keywords);

		// 5 转发
		request.getRequestDispatcher("news.jsp").forward(request, response);
	}

	/**
	 * 添加功能的操作
	 * 
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */

	private void doAdd(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// 1获取请求的参数,要先判断是否为空[需要有服务器端验证]
		String newstitle = request.getParameter("newstitle");
		String newscontent = request.getParameter("newscontent");
		int newstype = 1;
		if (request.getParameter("newstype") != null) {
			newstype = Integer.parseInt(request.getParameter("newstype"));
		}

		// 重复提交 -III 增加了一个判断是否为重复提交的操作
		String tokenSession = (String) request.getSession().getAttribute("token");
		String tokenRequest = request.getParameter("token");

		// 重复提交的判断
		if (tokenSession == null || tokenRequest == null || !tokenSession.equals(tokenRequest)) {
			// 重复提交了
			request.setAttribute("msg", "重复提交了!请勿刷新!");
			request.getRequestDispatcher("addnews.jsp").forward(request, response);
		} else {
			// 2构造一个News对象
			News news = new News(newstitle, newscontent, newstype);
			boolean flag = newsService.addNews(news);
			// 重复提交 IV 清空session内容
			request.getSession().removeAttribute("token");
			// 4根据返回结果进行转发或者提示等
			if (flag) {
				//
				request.setAttribute("msg", "增加成功！");
				request.getRequestDispatcher("addnews.jsp").forward(request, response);
			} else {
				// 转发到回到newstype增加的页面
				// 失败分两种情况，一种是如果名字重复；另外可能发生异常
				request.setAttribute("msg", "增加失败！请重新添加。");
				request.getRequestDispatcher("addnews.jsp").forward(request, response);

			}

		}

	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
